
import Vue from "vue";

import {Activity} from "./activity.js";
import {audioRecorder} from "../hos/audioRecorder";
import {wifi} from "../hos/wifi";
import {os} from "../hos/os";
import {tai} from "../hos/tai";

class AudioRecorder extends Activity{

    onCreate() {
        super.onCreate();

        // $(this.find("log")).append("WiFi信息:" + JSON.stringify(wifi.currentApInfo()) + "<br>")

        new Vue({
            el: this.find("start"),
            methods: {
                click: ()=> {
                    os.makeToast("开始录音");
                    audioRecorder.requestAuthorization().then(()=>{
                        audioRecorder.start().then((r) => {
                            tai.doHttpPost("https://api.ai.qq.com/fcgi-bin/aai/aai_asr", r).then(
                                (tr)=>{alert(JSON.stringify(tr));},
                                (e)=>{alert(JSON.stringify(e));});
                            // alert(JSON.stringify(r))
                        }).catch((e)=>{
                            alert(JSON.stringify(e));
                        });
                    }).catch((e) => {alert(JSON.stringify(e));});
                }
            }});

        new Vue({
            el: this.find("stop"),
            methods: {
                click: ()=> {
                    os.makeToast("即将停止录音");
                    audioRecorder.stop();
                }
            }});

        new Vue({
            el: this.find("play"),
            methods: {
                click: ()=> {
                    os.makeToast("即将播放录音");
                    audioRecorder.play();
                }
            }});
    }

    onResume() {
        super.onResume();
    }

    onDestroy() {
        super.onDestroy();
        audioRecorder.stop();
    }
}

export {AudioRecorder};
